package com.itheima.stock.service.impl;

import com.itheima.stock.entity.StockBusiness;
import com.itheima.stock.entity.StockMarketIndexInfo;
import com.itheima.stock.entity.StockMarketLogPrice;
import com.itheima.stock.mapper.StockBusinessMapper;
import com.itheima.stock.mapper.StockMarketIndexInfoMapper;
import com.itheima.stock.mapper.StockMarketLogPriceMapper;
import com.itheima.stock.propertis.MarketProperties;
import com.itheima.stock.service.StockService;
import com.itheima.stock.util.DateTimeUtil;
import com.itheima.stock.vo.InnerMarketVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class StockServiceImpl implements StockService {
    private final StockBusinessMapper stockBusinessMapper;
    private final StockMarketIndexInfoMapper stockMarketIndexInfoMapper;
    private final StockMarketLogPriceMapper stockMarketLogPriceMapper;
    private final MarketProperties marketProperties;

    public StockServiceImpl(StockBusinessMapper stockBusinessMapper, StockMarketIndexInfoMapper stockMarketIndexInfoMapper, StockMarketLogPriceMapper stockMarketLogPriceMapper, MarketProperties marketProperties) {
        this.stockBusinessMapper = stockBusinessMapper;
        this.stockMarketIndexInfoMapper = stockMarketIndexInfoMapper;
        this.stockMarketLogPriceMapper = stockMarketLogPriceMapper;
        this.marketProperties = marketProperties;
    }

    @Override
    public List<StockBusiness> getStockBusiness() {
        return stockBusinessMapper.selectAll();
    }

    @Override
    public List<InnerMarketVO> innerIndexAll() {
        //获取大盘编码
        List<String> ids = marketProperties.getInner();
        //获取最近有效交易时间
        LocalDateTime last = DateTimeUtil.getLastDateTime4Stock(LocalDateTime.now());
        String mockDate = "20211226105600";
        last = LocalDateTime.parse(mockDate, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        List<StockMarketIndexInfo> stockMarketIndexInfoList = stockMarketIndexInfoMapper.selectByIdsAndTime(ids, last);
        List<StockMarketLogPrice> stockMarketLogPriceList = stockMarketLogPriceMapper.selectByIdsAndDate(ids, last.toLocalDate());

        return stockMarketIndexInfoList.stream().map((info)->{
            InnerMarketVO vo = InnerMarketVO.builder().build();
            vo.setCode(info.getMarkId());
            vo.setName(info.getMarkName());
            vo.setCurDate(info.getCurTime().toLocalDate().toString());
            vo.setTradeAmt(info.getTradeAccount());
            vo.setOpenPrice(info.getCurrentPrice());
            vo.setTradeVol(info.getTradeVolume());
            vo.setUpDown(info.getUpdownRate());
            stockMarketLogPriceList.forEach((logPrice) ->{

                if(info.getMarkId().equals(logPrice.getMarketCode())){
                    vo.setOpenPrice(logPrice.getOpenPrice());
                    vo.setPreClosePrice(logPrice.getPreClosePrice());
                }else {
                    vo.setOpenPrice(BigDecimal.ZERO);
                    vo.setPreClosePrice(BigDecimal.ZERO);
                }
            });
            return vo;
        }).toList();
    }


}

